package com.ruoyi.system.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import com.ruoyi.common.utils.WeChatUtils;
import com.ruoyi.system.domain.ACustomeruser;
import com.ruoyi.system.service.IACustomeruserService;
import com.ruoyi.system.service.ICustomerBusinessService;
import com.ruoyi.system.utils.LoginUtils;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 48824
 */
@Service
public class CustomerBusinessServiceImpl implements ICustomerBusinessService {

    @Autowired
    private IACustomeruserService iaCustomeruserService;


    @Override
    public String loginUser(ACustomeruser aCustomeruser) {
        System.out.printf("a");
        try {
            final WxMaService wxMaService = WeChatUtils.getMiniWxMaService();
            //获取openId
            WxMaJscode2SessionResult session = wxMaService.getUserService().getSessionInfo(aCustomeruser.getWxPaymentCode());
            WxMaUserInfo userInfo = wxMaService.getUserService().getUserInfo(session.getSessionKey(), aCustomeruser.getZfbPaymentCode(),aCustomeruser.getInviteCode());
            System.out.println(userInfo+"sssada");
            Assert.notNull(session,"获取失败，请重新授权");
            WxMaPhoneNumberInfo phoneNoInfo = wxMaService.getUserService().getPhoneNoInfo(session.getSessionKey(),aCustomeruser.getZfbPaymentCode(),aCustomeruser.getInviteCode());
            Assert.notNull(phoneNoInfo.getPhoneNumber(), "获取手机号失败");
            ACustomeruser aCustomeruser1 = iaCustomeruserService.selectOpenId(session.getOpenid());
            if (null == aCustomeruser1){ //openId为空 新用户
                aCustomeruser1 = new ACustomeruser();
                aCustomeruser1.setNickName("微信名称");
                aCustomeruser1.setOpenId(session.getOpenid());
                aCustomeruser1.setPhonenumber(phoneNoInfo.getPhoneNumber());
                aCustomeruser1.setAvatar("");//头像
                iaCustomeruserService.insertACustomeruser(aCustomeruser1);
            }else {//openId不为空 老用户
                aCustomeruser1.setNickName(aCustomeruser.getNickName());
                aCustomeruser1.setAvatar(aCustomeruser.getAvatar());
                iaCustomeruserService.updateACustomeruser(aCustomeruser1);
            }
            return LoginUtils.login(aCustomeruser1.getOpenId());
        }catch (Exception e){
            return "登陆出现错误"+e;
        }
    }

    @Override
    public String getOpenId(String code) throws Exception {
        final WxMaService wxMaService = WeChatUtils.getMiniWxMaService();
        //获取openId
        WxMaJscode2SessionResult session = wxMaService.getUserService().getSessionInfo(code);
        return session.getOpenid();
    }

    @Override
    public int updateACustomeruser(ACustomeruser aCustomeruser) {
        return iaCustomeruserService.updateACustomeruser(aCustomeruser);
    }
}
